package top.leinx.fxnotes.stage;

/**
 * @Author : xiaolei
 * @DateTime: 2021/3/11 9:19 下午
 */

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class StateEventExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("窗口生命周期事件示例");

        primaryStage.setScene(new Scene(new Pane(), 300, 200));

        primaryStage.setOnCloseRequest(windowEvent -> {
            System.out.println("准备关闭窗口...");
        });

        primaryStage.setOnHiding(windowEvent -> {
            System.out.println("准备隐藏窗口...");
        });

        primaryStage.setOnHidden(windowEvent -> {
            System.out.println("窗口被隐藏了...");
        });

        primaryStage.setOnShowing(windowEvent -> {
            System.out.println("准备显示窗口...");
        });

        primaryStage.setOnShown(windowEvent -> {
            System.out.println("窗口已经显示...");
        });

        primaryStage.requestFocus();  // 要求获得输入焦点
        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, keyEvent -> {
            System.out.println("被按下的键: " + keyEvent.toString());
            switch (keyEvent.getCode().getCode()) {
                case 27:  // ESC
                    primaryStage.close();
                    break;
                case 10:  // Enter
                    System.out.println("Enter pressed!");
                    break;
                default:
                    System.out.println("无法识别的按键!");
            }
        });

        primaryStage.show();
    }
}
